#
# @lc app=leetcode.cn id=503 lang=python
#
# [503] 下一个更大元素 II
#

# @lc code=start
class Solution(object):
    def nextGreaterElements(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        result = [-1]*len(nums)
        stack = []
        for i in range(len(nums)*2):
            while stack and nums[i % len(nums)] > nums[stack[-1]]:
                result[stack[-1]] = nums[i % len(nums)]
                stack.pop()
            if i < len(nums):
                stack.append(i)
 
        return result
        
# @lc code=end

